<?php 
namespace app\supermanager\controller;

use think\Validate;
use think\Session;
use think\Controller;
/**
 * 找回密码控制器类
 */
class ManagerForgotPwd extends Controller
{
	protected $beforeActionList = [
        'postCode' => ['only'=>'getresetpwd'],
    ];
	/**
	 * 显示确认账号表单
	 */
	public function getIndex(){
		return view('managerforgotpwd/fotgotpwd');
	}
	
	 
	/**
	 * 获取手机验证码表单
	 */
	public function postCode(){
		if(input('post.imgCode')===null){
        	$this->redirect('/managerfotgot/index');
        }
		if (!captcha_check(input('post.imgCode'))) {
			return json($this->returnAjax(0,'图片验证码错误'));
        } else{
        	session('checkCode.imgCode',input('post.imgCode'));
        }
		if(!model('Managers')->getAllManagerInfo(['account' => input('post.account')])){
			return json($this->returnAjax(0,'账号错误'));
		}
		
		if(model('PhoneCode')->Send(input('post.account'))){
			return json($this->returnAjax(0,'手机验证码已发送，请注意查收'));
		}
	}
	
	/**
	 * 验证手机验证码
	 */
	public function postCheckCode(){
		if (session('checkCode.imgCode') != input('post.imgCode')) {
			return json($this->returnAjax(0,'验证码错误'));
        }else if(!$data = model('Managers')->getAllManagerInfo(['account' => input('post.account')])){
        	return json($this->returnAjax(0,'账号错误'));
        }
		
		if(model('PhoneCode')->Check(input('post.mobileCode'),session_id()) !== true){
			return json($this->returnAjax(0,Model('PhoneCode')->getError()));
		}else{
			return json($this->returnAjax(1,'验证通过',$data->id));
		}
	}
	
	/**
	 * 重置密码表单
	 */
	public function getResetPwd(){
	    return view('managerforgotpwd/resetpwd',['id'=>input('get.num')]);
	} 
	/**
	 * 重置密码信息
	 */
	public function postResetPwd(){
		if(false == Validate::is(input('post.password'), 'require')){
			return json($this->returnAjax(0,'密码不能为空'));
		}
		if(input('post.password') != input('post.confirmPwd')){
			return json($this->returnAjax(0,'两次输入的密码不一致'));
		}else if(model('Managers')->where('id',input('post.id'))->update(['password'=>input('post.password','','md5')])){
			return json($this->returnAjax(1,'修改密码成功，请登录'));
		}
		return json($this->returnAjax(0,'修改密码失败'));
	}
	
	/**
	 * ajax返回的数据
	 */
	public function returnAjax($num,$msg,$param=''){
		return ['status'=>$num,'message'=>$msg,'param'=>$param];		
	}
} 
 ?>
 










